package cn.hospital.seviceimpl;


import cn.hospital.dao.MeaningUserMapper;


import cn.hospital.entity.MeaningUser;

import cn.hospital.service.MeaningUserService;
import cn.hospital.util.MD5;
import cn.hospital.util.PageBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MeaningUserServiceImpl implements MeaningUserService {
    @Resource
    private MeaningUserMapper MeaningUserMapper;
    /**
     * 分页
     * @param meaningUser
     * @return
     */
    @Override
    public void selectuser(MeaningUser meaningUser, PageBean page) {
        page.setPageSize(5);
        System.out.println(meaningUser);
        int totalCount =MeaningUserMapper.selectusercount(meaningUser);
        page.setTotalCount(totalCount);
        Map<String, Object> map = new HashMap();
        map.put("startIndex",page.getStartIndex());
        map.put("pageSize", page.getPageSize());
        map.put("meaningUser",meaningUser);
        System.out.println(map);

        page.setList(MeaningUserMapper.selectuserlistbymap(map));
    }

    /**
     * 通过id删除用户
     * @param id
     * @return
     */
    @Override
    @Transactional
    public int delMeaningUser(Integer id){

        return MeaningUserMapper.delMeaningUser(id);
    }

    /**
     *通过id查询用户信息回显
     */
    public MeaningUser selectuserbyid(Integer id){
        return MeaningUserMapper.selectuserbyid(id);
    }

    /**
     * 保存修改信息通过id
     * @param
     * @return
     */
    @Transactional
    public int upMeaningUser(MeaningUser meaningUser){
        if (meaningUser.getPassword()!=null){
            meaningUser.setPassword(MD5.getMd5(meaningUser.getPassword(), 32));
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            meaningUser.setUserDatetime( sdf.parse(meaningUser.getUserDatetimeStr()) );
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return MeaningUserMapper.upMeaningUser(meaningUser);
    }

    //通过科室id查询用户列表
    @Override
    public List<MeaningUser> selectuserlist(Integer id) {
        return MeaningUserMapper.selectuserlist(id);
    }


}
